﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using GetuiServerApiSDK.payload;

namespace com.igetui.api.openservice.payload
{
	// Token: 0x0200003B RID: 59
	public class APNPayload : Payload
	{
		// Token: 0x1700021E RID: 542
		// (get) Token: 0x060004DC RID: 1244 RVA: 0x0000BA66 File Offset: 0x00009C66
		// (set) Token: 0x060004DD RID: 1245 RVA: 0x0000BA6E File Offset: 0x00009C6E
		public Dictionary<string, object> CustomMsg
		{
			get
			{
				return this.customMsg;
			}
			set
			{
				this.customMsg = value;
			}
		}

		// Token: 0x060004DE RID: 1246 RVA: 0x0000BA77 File Offset: 0x00009C77
		public void addCustomMsg(string key, object value)
		{
			if (key != null && key.Length > 0 && value != null)
			{
				this.customMsg.Add(key, value);
			}
		}

		// Token: 0x1700021F RID: 543
		// (get) Token: 0x060004DF RID: 1247 RVA: 0x0000BA95 File Offset: 0x00009C95
		// (set) Token: 0x060004E0 RID: 1248 RVA: 0x0000BA9D File Offset: 0x00009C9D
		public int Badge
		{
			get
			{
				return this.badge;
			}
			set
			{
				if (value > 0)
				{
					this.badge = value;
				}
			}
		}

		// Token: 0x17000220 RID: 544
		// (get) Token: 0x060004E1 RID: 1249 RVA: 0x0000BAAA File Offset: 0x00009CAA
		// (set) Token: 0x060004E2 RID: 1250 RVA: 0x0000BAB2 File Offset: 0x00009CB2
		public string Sound
		{
			get
			{
				return this.sound;
			}
			set
			{
				if (value == null || value.Length == 0)
				{
					this.sound = "default";
					return;
				}
				this.sound = value;
			}
		}

		// Token: 0x17000221 RID: 545
		// (get) Token: 0x060004E3 RID: 1251 RVA: 0x0000BAD2 File Offset: 0x00009CD2
		// (set) Token: 0x060004E4 RID: 1252 RVA: 0x0000BADA File Offset: 0x00009CDA
		public int ContentAvailable
		{
			get
			{
				return this.contentAvailable;
			}
			set
			{
				if (value > 0)
				{
					this.contentAvailable = value;
				}
			}
		}

		// Token: 0x17000222 RID: 546
		// (get) Token: 0x060004E5 RID: 1253 RVA: 0x0000BAE7 File Offset: 0x00009CE7
		// (set) Token: 0x060004E6 RID: 1254 RVA: 0x0000BAEF File Offset: 0x00009CEF
		public string Category
		{
			get
			{
				return this.category;
			}
			set
			{
				this.category = value;
			}
		}

		// Token: 0x17000223 RID: 547
		// (get) Token: 0x060004E7 RID: 1255 RVA: 0x0000BAF8 File Offset: 0x00009CF8
		// (set) Token: 0x060004E8 RID: 1256 RVA: 0x0000BB00 File Offset: 0x00009D00
		public AlertMsg AlertMsg
		{
			get
			{
				return this.alertMsg;
			}
			set
			{
				this.alertMsg = value;
			}
		}

		// Token: 0x17000224 RID: 548
		// (get) Token: 0x060004E9 RID: 1257 RVA: 0x0000BB09 File Offset: 0x00009D09
		// (set) Token: 0x060004EA RID: 1258 RVA: 0x0000BB11 File Offset: 0x00009D11
		public List<MultiMedia> MultiMedias
		{
			get
			{
				return this.multiMedias;
			}
			set
			{
				this.multiMedias = value;
			}
		}

		// Token: 0x060004EB RID: 1259 RVA: 0x0000BB1C File Offset: 0x00009D1C
		public string getPayload()
		{
			string result;
			try
			{
				Dictionary<string, object> dictionary = new Dictionary<string, object>();
				if (this.alertMsg != null && APNPayload.isNotEmpty(this.alertMsg.getAlertMsg()))
				{
					dictionary.Add("alert", this.alertMsg.getAlertMsg());
				}
				if (this.badge >= 0)
				{
					dictionary.Add("badge", this.badge);
				}
				if ("com.gexin.ios.silence" != this.sound)
				{
					dictionary.Add("sound", this.sound);
				}
				if (!APNPayload.isNotEmpty(dictionary))
				{
					throw new Exception("format error");
				}
				if (this.contentAvailable > 0)
				{
					dictionary.Add("content-available", this.contentAvailable);
				}
				if (APNPayload.isNotEmpty(this.category))
				{
					dictionary.Add("category", this.category);
				}
				Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
				foreach (KeyValuePair<string, object> keyValuePair in this.customMsg)
				{
					dictionary2.Add(keyValuePair.Key, keyValuePair.Value);
				}
				dictionary2.Add("aps", dictionary);
				if (this.apnsCollapseId != null)
				{
					dictionary2.Add("apns-collapse-id", this.apnsCollapseId);
				}
				if (this.multiMedias != null && this.multiMedias.Count > 0)
				{
					dictionary2.Add("_grinfo_", this.checkMultiMedias());
				}
				result = new JavaScriptSerializer().Serialize(dictionary2);
			}
			catch (Exception innerException)
			{
				throw new Exception("create apn payload error", innerException);
			}
			return result;
		}

		// Token: 0x060004EC RID: 1260 RVA: 0x0000BCDC File Offset: 0x00009EDC
		public void setApnsCollapseId(string apnsCollapseId)
		{
			if (apnsCollapseId != null && apnsCollapseId.Length > 0)
			{
				this.apnsCollapseId = apnsCollapseId;
			}
		}

		// Token: 0x060004ED RID: 1261 RVA: 0x0000BCF4 File Offset: 0x00009EF4
		private List<MultiMedia> checkMultiMedias()
		{
			if (this.multiMedias.Count > 3)
			{
				throw new Exception("MultiMedias size overlimit");
			}
			bool flag = false;
			HashSet<string> hashSet = new HashSet<string>();
			foreach (MultiMedia multiMedia in this.multiMedias)
			{
				if (multiMedia.rid == null)
				{
					flag = true;
				}
				else
				{
					hashSet.Add(multiMedia.rid);
				}
				bool flag2 = false;
				foreach (object obj in Enum.GetValues(typeof(MultiMedia.MediaType)))
				{
					if (((MultiMedia.MediaType)obj).Equals(multiMedia.type))
					{
						flag2 = true;
					}
				}
				if (!flag2 || multiMedia.url == null)
				{
					throw new Exception("MultiMedia resType and resUrl can't be null");
				}
			}
			if (hashSet.Count != this.multiMedias.Count)
			{
				flag = true;
			}
			int num = 0;
			if (flag)
			{
				foreach (MultiMedia multiMedia2 in this.multiMedias)
				{
					multiMedia2.rid = "grid-" + num++;
				}
			}
			return this.multiMedias;
		}

		// Token: 0x060004EE RID: 1262 RVA: 0x0000BE84 File Offset: 0x0000A084
		private static bool isNotEmpty(object item)
		{
			return item != null && ((item is string && (string)item != "") || (item is Array && ((Array)item).Length > 0) || (item is List<string> && ((List<string>)item).Count > 0) || (item is Dictionary<string, object> && ((Dictionary<string, object>)item).Count > 0));
		}

		// Token: 0x040002CE RID: 718
		public const string APN_SOUND_SILENCE = "com.gexin.ios.silence";

		// Token: 0x040002CF RID: 719
		public const int PAYLOAD_MAX_BYTES = 2048;

		// Token: 0x040002D0 RID: 720
		private string apnsCollapseId;

		// Token: 0x040002D1 RID: 721
		private List<MultiMedia> multiMedias = new List<MultiMedia>();

		// Token: 0x040002D2 RID: 722
		private Dictionary<string, object> customMsg = new Dictionary<string, object>();

		// Token: 0x040002D3 RID: 723
		private int badge = -1;

		// Token: 0x040002D4 RID: 724
		private string sound = "default";

		// Token: 0x040002D5 RID: 725
		private int contentAvailable;

		// Token: 0x040002D6 RID: 726
		private string category;

		// Token: 0x040002D7 RID: 727
		private AlertMsg alertMsg;
	}
}
